<?php 
require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/acc_conn.php';

function convert_strDate_to_iDate($strDate, &$iDate, $separator = "-") { //转换日期 "2019-5-7" => 2019*12*31 + 5*31 + 7
    $y = 0; $m = 0; $d = 0;
    $ret = convert_strDates_to_ymdDate($strDate, $y, $m, $d, $separator);
    if($ret) return $ret;
    $iDate = ($y * 12 + $m) * 31 + $d;
    return 0; 
}

function convert_iDates_to_strDate($iDate, &$strDate, $separator = "-") { //转换日期 2019*12*31 + 5*31 + 7 => "2019-5-7"
    $y = 0; $m = 0; $d = 0;
    $ret = convert_iDates_to_ymdDate($iDate, $y, $m, $d);
    if($ret) return $ret;
    $strDate = sprintf("%04d%s%02d%s%02d", $y, $separator, $m, $separator, $d);
    return 0; 
}

function convert_iDates_to_ymdDate($iDate, &$y, &$m, &$d) { //转换日期 2019*12*31 + 5*31 + 7 => $y:2019 $m:5 $d:7
    if(!isset($iDate)) return -1;
    if(!is_numeric($iDate)) return -2;
    
    $d = $iDate % 31;
    $m = floor(($iDate % (12 * 31)) / 31);
    $y = floor($iDate / (12 * 31));

	if($m < 1 || $m > 12) return -3;
    if($d < 1 || $d > 31) return -4;
    if($y < 1990) return -5;
    return 0; 
}

function convert_strDates_to_ymdDate($strDate, &$y, &$m, &$d, $separator = "-") { //转换日期 "2019-5-7" => $y:2019 $m:5 $d:7
    if(!isset($strDate)) return -1;
    $arr = explode($separator, $strDate);
    if(3 != count($arr)) return -2;
    
    $y = $arr[0];
    $m = $arr[1];
    $d = $arr[2];

    if(!is_numeric($y) || !is_numeric($m) || !is_numeric($d)) return -3;
	if($m < 1 || $m > 12) return -4;
    if($d < 1 || $d > 31) return -5;
    if($y < 1990) return -6;
    return 0; 
}
?>